#!/usr/bin/env python
# encoding: utf-8
# @Time    : 2019/10/12 15:04
# @Author  : ZRD
# @File    : checktask.py
import os, json
from .log import LOGGER


def checktaskdir(dirname):
    """
    检查文件下是否有任务
    """""
    try:
        for root, dirs, files in os.walk(dirname):

            for file in files:
                filepath = os.path.join(root, file)  # 文件路径
                name, suffix = os.path.splitext(file)  # 文件名、后缀名
                # print(f"文件名: {name} , 后缀名:{suffix}")
                if suffix == ".json":
                    f = open(filepath, 'r')
                    data = f.read()
                    taskdict = json.loads(data)
                    f.close()
                    return (taskdict, filepath)
            else:
                return (None, None)
    except Exception as e:
        LOGGER.error(e)
        return (None, None)


'''     
    
    for root, dirs, files in os.walk(fileDir):

        for dir in dirs:
            print(os.path.join(root, dir))

        for file in files:
            print(os.path.join(root, file))  # 文件路径
            name, suffix = os.path.splitext(file)  # 文件名、后缀名
            print(f"文件名: {name} , 后缀名:{suffix}")
'''

if __name__ == '__main__':
    print(checktaskdir('''C:\\Users\\zrd\\PycharmProjects\\fabu-agent\\tmpdir'''))
